﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guoc.DAL.Domain;
using Guoc.DAL.Interface;
using Guoc.DAL.CastleIOC;
using Guoc.Web.Biz;
namespace Guoc.Web.Controllers
{
    public class RoleController :Controller
    {

      //public  RoleController() : this(null) { }

      //public RoleController(RoleBiz irole)
      //  {
      //      _IRole = irole ?? new RoleBiz();
      //  }

        public RoleBiz  _IRole
        {
            get { return RoleBiz.Instance; }
           //private set ;
        }
      
       
        public ActionResult RoleIndex()
        {
            
            return View();
        }     
       
        /// <summary>
        ///  jquery easyui 分页，必须传递这2个参数
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="rows">每页记录行数</param>
        /// <returns></returns>
        public JsonResult GetRoleList(int page, int rows)
        {
            
            int count = 0;
            List<Role> list = this._IRole.GetRoleList(page,rows,out count);

            //返回如此Json对象
            return Json(new { total = count, rows = list });
        }

        /// <summary>
        ///  jquery easyui 分页，必须传递这2个参数
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="rows">每页记录行数</param>
        /// <returns></returns>
        public JsonResult QueryRoleList(string roleName, int page, int rows)
        {

            int count = 0;
            List<Role> list = this._IRole.QueryRoleList(roleName,page, rows, out count);

            //返回如此Json对象
            return Json(new { total = count, rows = list });
        } 


        [HttpPost]
        public ActionResult Create(string roleName,string remark)
        {

                Role role = new Role();               
                role.RoleName = roleName;
                role.Remark = remark;
                if (this._IRole.Create(role))
                {

                    return Json(new { code = 1, message = "新增成功！" });
                }
                else
                {

                    return Json(new { code = 0, message = "新增失败！" });
                }
           
        }
 
        //public ActionResult Edit1(string id)
        //{
        //    Role role = this._IRole.Get(id);
        //    return Json(role);
        //}

        [HttpPost]
        public ActionResult Edit(string id, string roleName,string remark)
        {
            try
            {
                Role role = new Role();
                role.ID = id;
                role.RoleName = roleName;
                role.Remark = remark;
                this._IRole.Update(role);

                return Json(new { code = 1, message = "修改成功！" });
            }
            catch
            {              
                return Json(new { code = 0, message = "修改失败！" });
            }
        }
        
        public ActionResult Delete(string ids)
        {
            if (ids.Length == 0)
            {
                return Json(new { code = 0, message = "删除失败！" }); 
            }

            try
            {               
                this._IRole.Delete(ids);

                return Json(new { code = 1, message = "删除成功！" });
            }
            catch
            {
                return Json(new { code = 0, message = "删除失败！" });
            }
        }

       
       
       
    }
}
